INDENT

Section: User Commands (1)
Updated: April 4, 1993
Index Return to Main Contents
 

NAME

indent - Indent and format C program source.

 

SYNOPSIS

indent [filename[s...]] [@listfile]

[-+|-n+] [-bacc|-nbacc] [-bad|-nbad] [-badp|-nbadp] [-bap|-nbap] [-bbb|-nbbb] [-bc|-nbc] [-br|-nbr] [-brr|-nbrr] [-bs|-nbs] [-cdb|-ncdb] [-ce|-nce] [-dj|-ndj] [-eei|-neei] [-ei|-nei] [-fc1|-nfc1] [-ip|-nip] [-ldefs|-nldefs] [-lp|-nlp] [-pcs|-npcs] [-pro|-npro] [-prs|-nprs] [-ps|-nps] [-psl|-npsl] [-sc|-nsc] [-sob|-nsob] [-st|-nst] [-tabu|-ntabu] [-troff|-ntroff] [-v|-nv]

[-c:n] [-cci:n] [-cd:n] [-ci:n] [-cli:n] [-cp:n] [-d:n] [-di:n] [-i:n] [-l:n] [-lc:n] [-tabs:n]

[-fb:...] [-fbc:...] [-fbx:...] [-fc:...] [-fk:...] [-fs:...] [-T:...]

 

DESCRIPTION

Indent is a C program formatter. It reformats the C program in the input-file according to the switches. Switches may appear before or after the file names.

If indent is run without any files or options on the command line, indent lists the default options and a summery of usage.

Multiple source files can be specified on the command line. Wild card filenames are not supported. @ indicates that the filename that follows is the name of a file list.

The formatting is done `in-place', that is, the formatted file is written back into input-file and a backup copy of input-file is written in the current directory. If input-file is named /blah/blah/file, the backup file is named /blah/blah/file.bak.

Options begin with -, and can appear anywhere in the command. These options control how programs are formatted.

 

PROFILE FILES

Indent defaults were set at compile time in args.c. These are overwridden successivly by profile files and the command line. The first set of changes may be set in argv[0].pro. You may set up your own `profile' of defaults to indent by creating a file called indent.pro in your login directory (pointed to by the environment variable HOME) and/or the current directory and including whatever switches you like. A `indent.pro' in the current directory takes precedence over the one in your login directory, which takes presidence over the one in the binary directory etc... If indent is run and a profile file exists, then it is read to set up the program's defaults. Switches on the command line, though, always override profile switches. The profile file switches should be separated by spaces, tabs or newlines.

 

COMMON FORMATTING STYLES

University of California at Berkeley defaults are:

-nbap -nbad -nbbb -bc -br -c:33 -cd33 -cdb -ce -ci:4 -cli:0 -d:4 -di:16 -fc1 -i:4 -ip:4 -l:75 -lp -npcs -psl -sc -nsob -fca -cp:33 -nss -tabu -tabs:8

The Kernighan & Ritchie style is the one used in their influential book The C Programming Language. Kernighan & Ritchie do not put comments to the right of code in the same column at all times (nor do they use only one space to the right of the code), so Indent in this mode has arbitrarily picked column 33. The Kernighan & Ritchie style corresponds to the following set of options:

-nbad -bap -nbbb -nbc -br -c:33 -cd:33 -ncdb -ce -ci:4 -cli:0 -d:0 -di:1 -nfc1 -i:4 -ip:0 -l:75 -lp -npcs -npsl -nsc -nsob -nfca -cp:33 -nss -tabu -tabs:8

The GNU coding style is the preferred style for writing code for the GNU project. It is also the style that the GNU emacs C mode encourages. To use GNU coding standards, specify the following options:

-pcs -psl -nsc -nsob -bli:2 -ss -cp:1 -nfca -cli:0 -tabu -tabs:8

 

OPTIONS

The options listed below control the formatting style imposed by indent.

-bad,-nbad
If bad is specified, a blank line is forced after every block of declarations.
-bap,-nbap
If -bap is specified, a blank line is forced after every procedure body.
-bbb,-nbbb
If -bbb is specified, a blank line is forced before every block comment.
-bc,-nbc
If -bc is specified, then a newline is forced after each comma in a declaration. -nbc turns off this option.
-br,-nbr-brr(-nbrwas-bl)
Specifying -bl lines up compound statements like this:
    if (...)
    {
        code
    }
Specifying -nbl makes them look like this:
    if (...) {
        code
    }
And specifying -brr makes them look like this:
    if (...)
        {
        code
        }
-c:n
Comments which appear on the same line as code are placed to the right of the code in column n.
-cci:n
Causes case code to be indented n spaces to the right of the corresponding case label.
-cd:n
The column in which comments on declarations start. The default is for these comments to start in the same column as those on code (-c:n).
-cdb,-ncdb
Enables (disables) the placement of comment delimiters on blank lines. With this option enabled, comments look like this:
        /*
         * this is a comment
         */
Rather than like this:
        /* this is a comment */
This only affects block comments, not comments to the right of code.
-ce,-nce
Enables (disables) forcing `else's to cuddle up to the immediately preceding `}'.
-ci:n
Sets the continuation indent to be n. Continuation lines will be indented that far from the beginning of the first line of the statement. Parenthesised expressions have extra indentation added to indicate the nesting, unless -lp is in effect. -ci defaults to the same value as -i.
-cli:n
Causes case labels to be indented n spaces to the right of the containing switch statement.
-cp:n
The column number for comments to the right of #else and #endif statements is controlled by the -cp option.
-d:n
Controls the placement of comments which are not to the right of code. -d:1 means that such comments are placed one indentation level to the left of code. Specifying -d:0 lines up these comments with the code. See the section on comment indentation below.
-di:n
Specifies the indentation, in spaces, from a declaration keyword to the following identifier.
-dj,-ndj
-dj left justifies declarations. -ndj indents declarations the same as code.
-ei,-nei
Enables (disables) special else-if processing. If it's enabled, an if following an else will have the same indentation as the preceding if statement.
-fc1,-nfc1
Enables (disables) the formatting of comments that start in column 1. Often, comments whose leading `/' is in column 1 have been carefully hand formatted by the programmer. In such cases, -nfc1 should be used.
-i:n
The number of spaces for one indentation level.
-ip,-nip
Enables (disables) the indentation of parameter declarations from the left margin.
-l:n
Maximum length of an output line.
-lp,-nlp
Lines up code surrounded by parenthesis in continuation lines. If a line has a left paren which is not closed on that line, then continuation lines will be lined up to start at the character position just after the left paren. For example, here is how a piece of continued code looks with -nlp in effect:
    p1 = first_procedure(second_procedure(p2, p3),
        third_procedure(p4, p5));
With -lp in effect (the default) the code looks somewhat clearer:
    p1 = first_procedure(second_procedure(p2, p3),
                         third_procedure(p4, p5));
Inserting a couple more newlines we get:
    p1 = first_procedure(second_procedure(p2,
                                          p3),
                         third_procedure(p4,
                                         p5));
-npro
Causes the profile files, argv[0].pro ./indent.pro and ~/indent.pro, to be ignored.
-pcs,-npcs
If true -pcs all procedure calls will have a space inserted between the name and the `('.
-prs,-nprs
If true (-prs) all parentheses will have a space inserted after the '(' and before the ')'.
-psl,-npsl
If true (-psl) the names of procedures being defined are placed in column 1 - their types, if any, will be left on the previous lines. This style (-psl) is required for the etags program to work correctly.
-sc,-nsc
Enables (disables) the placement of asterisks (`*'s) at the left edge of all comments.
-sob,-nsob
If -sob is specified, indent will swallow optional blank lines. You can use this to get rid of blank lines after declarations.
-st
Causes indent to take its input from stdin, and put its output to stdout.
-tabs:n
Tells indent that tabs are assumed to be at every n columns. If n is less than 2 then tabs will not be used at all in the output.
-tabu-ntabu
If -tabu is specified, indent will use tabs to indent output. If -ntabu is specified, indent only uses spaces in the output.
-Ttypename
Adds typename to the list of type keywords. Names accumulate: -T can be specified more than once. You need to specify all the typenames that appear in your program that are defined by typedef - nothing will be harmed if you miss a few, but the program won't be formatted as nicely as it should. This sounds like a painful thing to have to do, but it's really a symptom of a problem in C: typedef causes a syntactic change in the language and indent can't find all instances of typedef.
    For example, if your program contains:

    typedef unsigned long CODE_ADDR;
    typedef enum red, blue, green COLOR;

    you would use the options -T CODE_ADDR -T COLOR.
-troff
Causes indent to format the program for processing by troff. It will produce a fancy listing in much the same spirit as vgrind. Output is to standard output, rather than formatting in place.
-v,-nv
-v turns on `verbose' mode; -nv turns it off. When in verbose mode, indent reports when it splits one line of input into two or more lines of output, and gives some size statistics at completion. It also displays option and profile file processing. TP 10 -+ turns on support for C++. In c++ mode, :: is permited in identifiers, C++ keywords are supported, and class definition keywords (public, private, etc.) are set in column 2.

 

COMMENTS

If the code on a line extends past the comment column, the comment starts further to the right, and the right margin may be automatically extended in extreme cases.

 

Box comments.

Indent assumes that any comment with a dash or star immediately after the start of comment (that is, `/*-' or `/**') is a comment surrounded by a box of stars. Each line of such a comment is left unchanged, except that its indentation may be adjusted to account for the change in indentation of the first line of the comment.

 

Straight text

All other comments are treated as straight text. Indent fits as many words (separated by blanks, tabs, or newlines) on a line as possible. Blank lines break paragraphs.

 

Comment indentation

If a comment is on a line with code it is started in the `comment column', which is set by the -cn command line parameter. Otherwise, the comment is started at n indentation levels less than where code is currently being placed, where n is specified by the -dn command line parameter. If the code on a line extends past the comment column, the comment starts further to the right, and the right margin may be automatically extended in extreme cases.

 

Special Comments

Indent produces and interprets some special comments. When indent cannot parse the source, it prints a message on standard error and inserts a comment into the output of the form

        /**INDENT** ErrorMessage */

Indent interprets several special comments as directives. First, it makes no attempt to format lines containing the error comment described above.

Second, lines of the form:

        /* INDENT OFF */
or
        /* INDENT ON */
disable and re-enable indent formatting. Any amount of whitespace may replace the spaces shown in the examples.

Third, indent allows formatting controls to be included in the source via comments of the form:

        /* INDENT: arg1 arg2 arg3 ... arg4 */
The arguments given are in the same syntax as the command line or profile file. For example:
        /* INDENT: -cli.25 -nfc1 */

 

Preprocessor lines

In general, indent leaves pre-processor lines alone. The only reformatting that it will do is to straighten up trailing comments. It leaves embedded comments alone. Conditional compilation #ifdef... #endif is recognised and indent attempts to correctly compensate for the syntactic peculiarities introduced.

 

C SYNTAX

Indent understands a substantial amount about the syntax of C, but it has a `forgiving' parser. It attempts to cope with the usual sorts of incomplete and misformed syntax. In particular, the use of macros like: #define forever for(;;) is handled properly.

 

ENVIRONMENT

Indent uses the HOME environment variable.

 

FILES

argv[0].pro ./indent.pro ~/indent.pro and indent.pro are profile files.

 

DEFAULTS

The programme has a set of default options built in. These may be overridden, successively, by:

Options in argv[0].pro (any .xxx extention is stripped first), Options in ~/indent.pro (the path ~/ is set in the environment variable HOME), Options in ./indent.pro, Options on the command line.

 

HISTORY

Indent appeared in 4.2 BSD.

Peter Hadfield, 4-04-93.

Command line option scanning rewritten. Option names cannot now start with a simplified the argument parsing routine.

Several files can be specified on the command line or a list of files can be specified. Source code cleaned up and encapsulation improved. Ported to Vanilla C, now clean compiles with all the warnings turned on.

Options -ldefs, -tabu and -tabu added. Option -bl is now -nbr for consistency. Option -cli:n now takes an integer number of spaces, rather than a float (this simplifies command line processing).

indent.pro is no longer a UNIX hidden file for DOS compatibility.

Usage and current option display added.

Added C++ support by merging the C++ compatable code found in comp.sources.misc.

Added (most of) the improvements made by Jon Saxton who ported indent to OS2.

24-05-93 Fixed a bug whare comments following a single colon ':' are indented by n-1 spaces, for example in a "case :" statment. This is masked if we are using tabs in the output.

 

BUGS

Indent has even more switches than ls.

There might be a bug with -nbap whereby if the end of a procedure is followed by something such as the name of a type, with no intervening comment, the blank line will not be added.

 

TESTING

It is far too large a task to fully test all possible permutations of the options, however this is not a major problem, incorrect formating is not serious. Only errors resulting in a change to the final binary file are significant, testing can therefore be simplified.

A project should be compiled and the binary saved. The source code should be (deformated and) indented. The project is then recompiled and the binaries compared. They should be identical.


 

Index

NAME
SYNOPSIS
DESCRIPTION
PROFILE FILES
COMMON FORMATTING STYLES
OPTIONS
COMMENTS
Box comments.
Straight text
Comment indentation
Special Comments
Preprocessor lines
C SYNTAX
ENVIRONMENT
FILES
DEFAULTS
HISTORY
BUGS
TESTING

This document was created by man2html, using the manual pages.
Time: 23:18:51 GMT, September 29, 2022